import treeEach from 'operation-tree-node/dist/treeEach.esm';
/** 
  1.改写change事件 让change事件可返回原始data而不只有value值
  2.添加ollie的禁用规则 禁用样式去除 选中禁用node不做任何反应
*/
export const addNewChangeEvent = function (ElSelectTree) {
  ElSelectTree.props.ollieDisabled = Boolean

  ElSelectTree.methods.nodeClick = function nodeClick(data, node, component) {
    const children = data[this.props.children];
    const value = data[this.propsValue];
    const disabledValues = this.disabledValues;

    if (children && children.length && !this.checkStrictly) {
      component.handleExpandIconClick();
    } else if (!this.multiple && !data.disabled) {
      if (value !== this.value) {
        // 自定义禁用规则
        // 禁用的只打开下级
        if (disabledValues.includes(value) && this.ollieDisabled) {
          return component.handleExpandIconClick()
        }
        this.valueChange(value, data);
        this.selectedLabel = data[this.propsLabel];
      }

      this.visible = false;
    }
  }
  ElSelectTree.methods.valueChange = function valueChange(value, data) {
    this.$emit('change', value, data);
    this.dispatch('ElFormItem', 'el.form.change', value);
  }

  ElSelectTree.methods.setTreeDataState = function setTreeDataState() {
    if (this.ollieDisabled) return // 如果自定义的禁用规则 直接跳出
    const _this3 = this;

    const disabledValues = this.disabledValues;
    treeEach(this.data, function (node) {
      node.disabled = disabledValues.includes(node[_this3.propsValue]);

      if (node.disabled) {
        treeEach(node[_this3.propsChildren], function (childNode) {
          return childNode.disabled = true;
        });
        return false;
      }
    }, {
      children: this.propsChildren
    });
  }

}
export default {
  addNewChangeEvent
}